// This file is part of WifiCC3000 - the Managed Wifi Driver for the TI CC3000
// WifiCC3000 is free software licensed under the Apache License 2.0
// by Valkyrie-MT

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Networking;
using System.Text;

namespace WifiExampleApp
{
    public class DnsTest
    {
        static bool waiting = true;

        static byte[] cannedTI_MDNS = new byte[] {
 0x00, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x5f, 0x64, 0x65, 
 0x76, 0x69, 0x63, 0x65, 0x2d, 0x69, 0x6e, 0x66, 0x6f, 0x04, 0x5f, 0x75, 0x64, 0x70, 0x05, 0x6c, 
 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01, 0x00, 0x00, 0x11, 0x94, 0x00, 0x09, 0x06, 
 0x63, 0x63, 0x33, 0x30, 0x30, 0x30, 0xc0, 0x0c, 0x09, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 
 0x65, 0x73, 0x07, 0x5f, 0x64, 0x6e, 0x73, 0x2d, 0x73, 0x64, 0x04, 0x5f, 0x75, 0x64, 0x70, 0x05, 
 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01, 0x00, 0x00, 0x11, 0x94, 0x00, 0x02, 
 0xc0, 0x0c, 0xc0, 0x2f, 0x00, 0x10, 0x80, 0x01, 0x00, 0x00, 0x11, 0x94, 0x00, 0x24, 0x0a, 0x64, 
 0x65, 0x76, 0x3d, 0x43, 0x43, 0x33, 0x30, 0x30, 0x30, 0x18, 0x76, 0x65, 0x6e, 0x64, 0x6f, 0x72, 
 0x3d, 0x54, 0x65, 0x78, 0x61, 0x73, 0x2d, 0x49, 0x6e, 0x73, 0x74, 0x72, 0x75, 0x6d, 0x65, 0x6e, 
 0x74, 0x73, 0xc0, 0x2f, 0x00, 0x21, 0x80, 0x01, 0x00, 0x00, 0x11, 0x94, 0x00, 0x0f, 0x00, 0x00, 
 0x00, 0x00, 0x04, 0xd2, 0x06, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0xc0, 0x1e, 0xc0, 0xa4, 0x00, 
 0x01, 0x80, 0x01, 0x00, 0x00, 0x11, 0x94, 0x00, 0x04, 0xc0, 0xa8, 0x01, 0x52  };


        public static void Main()
        {
            Debug.Print("CC3000 DNS Test");

            var wifi = new WifiAdapter("CC3000", InterfaceProfile.CerbuinoBee_Socket1_CC3000);
            wifi.Connected += wifi_Connected;

            while (waiting) System.Threading.Thread.Sleep(50);

            wifi.Stop();
        }

        static void wifi_Connected(WifiAdapter wifiAdapter)
        {
            Debug.Print("Woot!  We are now connected to Wifi!");
            Debug.Print("Network Name: " + wifiAdapter.SsidName);
            Debug.Print("IP address: " + wifiAdapter.IPAddress.ToAddress());
            //Debug.Print("MacAddress: " + wifiAdapter.MacAddress.ToHexString());
            Debug.Print("Gateway: " + wifiAdapter.Gateway.ToAddress());
            Debug.Print("DNS Server: " + wifiAdapter.DomainNameServer.ToAddress());

            //wifiAdapter.SendDeviceAdvertisement();

            var s = wifiAdapter.OpenUdpSocket();

            while (true)
            {
                // MDNS
                s.Send(cannedTI_MDNS, new byte[] { 0xE0, 0x00, 0x00, 0xFB }, 5353); // using MDNS IP and port 224.0.0.251:5353

                // LLMNR
                // This is Wrong because the LLMNR packet must be sent to the requesting IP and port.  
                //s.Send(cannedTI_MDNS, new byte[] { 0xE0, 0x00, 0x00, 0xFC }, 5355); // using MDNS IP and port 224.0.0.252:5355

                System.Threading.Thread.Sleep(2000);
            }


            waiting = false;
        }
    }
}


